package cn.xorange.commons.system.service.System.impl;

import cn.xorange.commons.core.basic.service.BaseService;
import cn.xorange.commons.data.constants.DataConstants;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.SysUserRole;
import cn.xorange.commons.system.entity.system.*;
import cn.xorange.commons.system.mapper.system.SysUserRoleMapper;
import cn.xorange.commons.system.mapper.system.*;
import cn.xorange.commons.system.service.System.ISysRoleService;
import com.alibaba.fastjson2.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : yangjian
 * @date : 2022/2/22
 * @description :
 */
@Service
public class SysRoleServiceImpl extends BaseService<SysRole, SysRoleMapper> implements ISysRoleService {

    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    SysRoleOrgMapper sysRoleOrgMapper;
    @Resource
    SysPostMapper postMapper;
    @Resource
    SysRoleManageMapper roleManageMapper;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysRole> getList(SysRole sysRole){
        SecurityUser user = SecurityUtils.getUser();
        boolean isAdmin = user.getIsAdmin();
        sysRole.setTenantId(user.getTenantId());
        return mapper.selectRoleList(isAdmin, user.getUserId(), sysRole);
    }


    @Override
    public int checkRoleNameUnique(SysRole sysRole) {
        return mapper.checkRoleNameUnique(sysRole);
    }

    @Override
    public int checkRoleKeyUnique(SysRole sysRole) {
        return mapper.checkRoleKeyUnique(sysRole);
    }

    @Override
    public List<Long> getRoleMenuIds(Long roleId) {
        return sysRoleMenuMapper.selectRoleMenuIds(roleId);
    }
    @Override
    public List<Long> getRoleOrgIds(Long roleId){
        return this.sysRoleOrgMapper.selectRoleOrgIds(roleId);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void setRoleMenuIds(SysRole sysRole){
        JSONArray menuIds = sysRole.getParams().getJSONArray("menuIds");
        Map<String, Object> fields = new HashMap<>(1);
        fields.put("roleId", sysRole.getRoleId());
        setRelationIds(fields, menuIds, "menuId", sysRoleMenuMapper, SysRoleMenu.class);
    }

    public void setRoleOrgIds(SysRole sysRole){
        JSONArray orgIds = sysRole.getParams().getJSONArray("orgIds");
        Map<String, Object> fields = new HashMap<>(1);
        fields.put("roleId", sysRole.getRoleId());
        setRelationIds(fields, orgIds, "orgId", sysRoleOrgMapper, SysRoleOrg.class);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int add(SysRole sysRole){
        int num = mapper.insert(sysRole);
        if(DataConstants.DATA_SCOPE_CUSTOM.equals(sysRole.getDataScope())){
            this.setRoleOrgIds(sysRole);
        }
        this.setRoleMenuIds(sysRole);
        return num;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int update(SysRole sysRole){
        if(DataConstants.DATA_SCOPE_CUSTOM.equals(sysRole.getDataScope())){
            this.setRoleOrgIds(sysRole);
        }
        this.setRoleMenuIds(sysRole);
        return mapper.update(sysRole);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteById(Long roleId){
        // 删除role管理的role
        this.roleManageMapper.delRoleManageRoleId(roleId);
        // 删除role管理岗位
        this.postMapper.delPostRoleId(roleId);
        // 删除用户角色
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(roleId);
        this.sysUserRoleMapper.del(userRole);
        // 删除role 关联自定义部门
        SysRoleOrg roleOrg = new SysRoleOrg();
        roleOrg.setRoleId(roleId);
        this.sysRoleOrgMapper.del(roleOrg);
        // 删除role关联菜单
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(roleId);
        this.sysRoleMenuMapper.del(sysRoleMenu);
        // 删除角色
        return this.mapper.delById(roleId,SysRole.class);
    }

    @Override
    public String getRoleNames(Set<String> roleKeys) {
        return mapper.selectRoleNames(roleKeys);
    }

}
